#include "connection.hpp"
#include "ansyncpool.hpp"

using namespace ns_rabbitmq;
using Map = google::protobuf::Map<std::string, std::string>;
int main()
{
    // 1.创建线程池
    AnsyncPoolPtr pools = std::make_shared<AnsyncPool>();
    // 2.创建连接管理
    ConnectionPtr conn = std::make_shared<Connection>("127.0.0.1", 8085, pools);
    // 3.打开信道
    ChannelPtr channel = conn->OpenChannel();
    // 4.创建信道的服务
    //   1.交换机 2.队列 3.绑定 4.推送
    channel->DeclareExchange("exchange1", ExchangeType::FANOUT, true, false, Map());
    channel->DeclareExchange("exchange2", ExchangeType::DIRECT, true, false, Map());
    channel->DeclareExchange("exchange3", ExchangeType::TOPIC, true, false, Map());
    channel->DeclareQueue("queue1", false, true, false, Map());
    channel->DeclareQueue("queue2", false, true, false, Map());
    channel->Bind("exchange1", "queue1", "queue1", true);
    channel->Bind("exchange1", "queue2", "news.music.#", true);

    channel->Bind("exchange2", "queue1", "queue1", true);
    channel->Bind("exchange2", "queue2", "news.music.#", true);

    channel->Bind("exchange3", "queue1", "news.music.#", true);
    channel->Bind("exchange3", "queue2", "news.#", true);

    // 5.循环推送消息
    //预期queue1和queue2收到广播交换
    for (int i = 0; i < 2; i++)
    {
        BasicProperties bp1;
        bp1.set_id(UUIDHelp::uuid());
        bp1.set_deliver_mode(DeliveryMode::DURABLE);
        bp1.set_routing_key("news.music.pop");
        channel->BasicPublish("exchange1", &bp1, "广播交换" + std::to_string(i));
    }

    // 预期: queue1 收到直接推送 queue2不收到
    BasicProperties bp2;
    bp2.set_id(UUIDHelp::uuid());
    bp2.set_deliver_mode(DeliveryMode::DURABLE);
    bp2.set_routing_key(std::string("queue1"));
    channel->BasicPublish("exchange2", &bp2, "这是直接推送");

    // 预期: queue1 收到主题推送 queue2收到主题推送
    BasicProperties bp3;
    bp3.set_id(UUIDHelp::uuid());
    bp3.set_deliver_mode(DeliveryMode::DURABLE);
    bp3.set_routing_key(std::string("news.music.pop"));
    channel->BasicPublish("exchange3", &bp3, "这是主题推送");

    // 6.关闭信道
    conn->CloseChannel(channel);
    return 0;
}